//#pragma  once
#ifndef TCP_RUITU_H
#define TCP_RUITU_H

#include <unistd.h>
#include <typeinfo>
#include <string>
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <string>
#include <iomanip>
#include <functional>
#include <stdio.h>
#include <time.h>
#include <mutex>

#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
//#include <conio.h>              //for _kbhit(),but in linux ,it don't use

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/shm.h>
#include <sys/time.h>           //  for function ->  gettimeofday(&tval, NULL);


#include <errno.h>

//#include <json/json.h>
//#include <json/value.h>
//#include <json/reader.h>
//#include <json/writer.h>
#include <json.h>
#include <netinet/tcp.h>

#include <algorithm>
#include <deque>

#include "config.h"
#include "generater.h"
//#include "picture2string.h"



class client_for_result
{
public:
    client_for_result(const std::string &road_to_config,int connet_max, std::string id);
    ~client_for_result();

    int client_sockfd;
	int len;
	struct sockaddr_in remote_addr;
    char recv_buf[5000];//数据接收缓冲区
   	char send_buf[5000];//数据传输缓冲区

    bool ctrl_heart_beat;
    bool ctrl_alarm;
    bool ctrl_check_heart_beat;
    bool judge_connect;

    std::mutex *lock_for_judge_connect = new std::mutex;
    bool judgment();
    std::mutex *lock_for_ctrl_ptr = new std::mutex;
    std::string this_road_to_config;
    int connet_max_number;
    
    bool ready_for_connect();
    bool ready_for_socket();
    bool connect_to_server();
    void loop();
    
    void outloop();

    bool reconnect(); 

    //for heart beat
    void generate_thread_for_heartbeat();
    bool heart_beat();
    void check_heartbeat();
    std::mutex *lock_for_heart_beat_ptr = new std::mutex;
    int count_heartbeat;

    std::mutex *lock_for_sending = new std::mutex;
    //for alarm
    bool send_alarm();
    void generate_thread_for_alarm();

    //for comewith
    //void come_with_server();
    //void feed_in_deque(std::string &sending);
    void feed_in(std::string & sending);

    std::string helloACK_rec;
    std::string heartbeat_rec;

    //alarm
    std::string alarm;
    std::string device_info;
    std::string helloACK;
    std::string heartbeat;
    
    //id
    std::string robotid;
    

    //try ptr
    std::thread *check_heartbeat_thread_ptr;
    std::thread *send_alarm_thread_ptr;
    std::thread *heartbeat_thread_ptr;
    //std::thread *come_with_server_ptr;

    //config
    param_config * ip_ptr = new param_config;
    param_config * overtime_ptr = new param_config;

    //generater
    generater * generater_js = new generater;
    
    //generate pic
    //picture2string * test_picture_str_ptr = new picture2string;

    //informtion deque
    std::deque<std::string> alarm_and_info;
    std::mutex *lock_for_deque_ptr = new std::mutex;
};

#endif
